<?php
/**
 * 背包
 * 
 * Created	: 2010-10-11
 * Modified : 2010-10-15
 * @author  : 陈立强
 */
class Bag
{
	
	public static $itemList = array();
	public static $_bag_id = 0;
	public static $msg = 'ss';
	
	/**
	 * 读取用户仓库物品列表
	 * @param int $u_id 用户ID
	 * @param int|string $type 仓库物品类型
	 */
	public static function getList($uid, $type=0, $status=1)
	{
		global $DB, $L, $G;
		
		$type = (int)$type;
		$status = (int)$status;
		
		$pageSize = $G['bag_grid_page'];
		$list = array();
		$where = 'uid='.$uid.' and status='.$status.' ';
		if (is_int_string($type) && $type>0) {
			$where .= ' and item_type='.(int)$type;
		} elseif (preg_match('~^(?:[\d]+\,)+[\d]+$~is', $type)) {
			$where .= ' and item_type in ('.$type.')';
		}
		$curNum = $DB->getCount('bag', 'uid='.$uid);
		$maxNum = $DB->getValue('SELECT bag_max_grid FROM user WHERE uid='.$uid);
		$total = $DB->getCount('bag', $where);
		$sql = 'SELECT * FROM bag WHERE '.$where;
		$arr = $DB->getList($sql);
		$equipCfgList = C('item/equip');
		if (false === $arr) {
			return array(
				'val' => false,
				'msg' => $L['Failure']
			);
		}
		// 物品配置列表
//		$itemCfgList = C('item/item_list');
		$gemCfgList = C('item/gem');
		foreach ($arr as $val) {
			// 解析属性
			if ($val['attr']) {
				$val['attr'] = json_decode($val['attr'], true)  ;
			}
			if ($val['item_type'] == 53) { // 装备处理
				$cfg = $equipCfgList[$val['item_id']];
				$val['make_class'] = $cfg['class'];
				$val['need_staff_lvl'] = $cfg['need_staff_lvl'];
				$val['inlay'] = json_decode($val['inlay'], true);
			} else if ($val['item_type'] == 52) {//宝石处理
				$gem_abi_id = key($val['attr']);
				$val['class'] = $gemCfgList[$val['item_id']]['class'];
			}
			$list[] = $val;
		}
		
		return array(
			'val'  => true,
			'data' => $list
		);
	}
	
	/**
	 * 获取所有物品类别名称列表
	 * @param int $uid 用户ID,用户接口请求默认当前登录用户
	 */
	public static function getItemTypeList($uid)
	{
		$list = C('item/item_type_name_bag');
		
		return array(
			'val'  => true,
			'data' => $list
		);
	}
	
	/**
	 * 物品存入仓库
	 * @param int $uid 用户ID
	 * @param array $data 物品进入仓库的一些参数item_id,num,quality
	 */
	public static function in($uid, $data)
	{
		global $DB, $L, $G;
		$freeGrid = self::getFreeGrid($uid);
		if ($freeGrid <= 0) {
			self::$msg = $L['not_enough_grid_in_bag'];
			return false;
		}
		
		$itemId		= (int)$data['item_id'];
		$itemType	= isset($data['item_type']) ? (int)$data['item_type'] : 0;
		$stack		= isset($data['stack']) ? (int)$data['stack'] : 0;
		$lvl		= isset($data['lvl']) ? (int)$data['lvl'] : 0;
		$staff_id	= isset($data['staff_id']) ? (int)$data['staff_id'] : 0;
		$status		= isset($data['status']) ? (int)$data['status'] : 1;
		$part		= isset($data['part']) ? $data['part'] : 0;
		$num     	= (int)$data['num'];
		$quality 	= isset($data['quality']) ? (int)$data['quality'] : 0;
		$color	 	= isset($data['color']) ? (int)$data['color'] : 0;
		$inlay		= isset($data['inlay']) ? $data['inlay'] : '';
		$attr		= isset($data['attr']) ? $data['attr'] : '';

		if (empty(self::$itemList)) {
			$titems   = C('item/item_list');
			$itemInfo = $titems[$itemId];
			self::$itemList = $titems;
		}
		if (isset($data['title'])) {
			$title = $data['title'];
		} else {
			$title = $itemInfo['title'];
		}
		
		$where = 'uid=:uid and item_id=:item_id and quality=:quality';
		$params = array(
			'uid'     => $uid,
			'item_id' => $itemId,
			'quality' => $quality
		);
		if ($stack) {
			$sql = 'SELECT bag_id,num,stack FROM bag WHERE '.$where;
			$bagInfo = $DB->getValue($sql, $params);
			$formerNum = (int)$bagInfo['num'];
			$num += $formerNum;
			if ($num > $G['item_max_stack']) {
				self::$msg = $L['item_is_overflow'];
				return false;
			}
		}
		
		if (!$stack || !$bagInfo || !$bagInfo['stack']) {
			$data = array(
				'uid'		=> $uid,
				'item_id'	=> $itemId,
				'item_type' => $itemType,
				'title'		=> $title,
				'num'		=> $num,
				'quality'	=> $quality,
				'stack'		=> $stack,
				'lvl'		=> $lvl,
				'color'		=> $color,
				'part'		=> $part,
				'staff_id'	=> $staff_id,
				'status'	=> $status,
				'inlay'		=> $inlay,
				'attr'		=> $attr,
			);
			$ret = $bag_id = $DB->insert('bag', $data);
		} else {
			$data = array(
				'num' => $num
			);
			$where = 'bag_id='.$bagInfo['bag_id'];
			$ret = $DB->update('bag', $data, $where);
			$bag_id = $bagInfo['bag_id'];
		}
		
		self::$msg = '';
		if ($ret) {
			self::$_bag_id = $bag_id;
		} else {
			$s = $itemId.': '.$title;
			$data = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('bag_in', $data);
		}
		return $ret;
	}
	
	/**
	 * 物品出库
	 * @param int $uid
	 * @param int $data array($bag_id => $num)
	 */
	public static function out($uid, $data)
	{
		global $DB, $L;
		if (empty($data)) {
			return array(
				'val' => false,
				'msg' => $L['bag_out_item_not_enough']
			);
		}
		$ids = array_keys($data);
		$ids = join(',', $ids);
		$sql = 'SELECT * FROM bag WHERE bag_id in ('.$ids.')';
		$list = $DB->getList($sql);
		if (empty($list)) {
			return array(
				'val' => false,
				'msg' => $L['bag_out_item_not_enough']
			);
		}
		$sql = '';
		foreach ($list as $v) {
			$num = $data[$v['bag_id']];
			if ($v['num'] < $num) {
				return array(
					'val' => false,
					'msg' => $L['bag_out_item_not_enough']
				);
			}
			$v['num'] -= $num;
			$where = 'bag_id='.$v['bag_id'];
			$sql  .= 'UPDATE bag SET num='.$v['num'].' WHERE '.$where.';';
		}
		$resultVal = $DB->exec($sql);
		if ($resultVal !== false) {
			$resultVal = true;
		}
		
		$result = array('val' => $resultVal);
		if ($resultVal) {
		} else {
			$result['msg'] = $L['Failure'];
			$data = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('bag_out', $data);
		}
		return $result;
	}
	
	/**
	 * 指定玩家的仓库剩余格子
	 * @param int $uid
	 */
	public static function getFreeGrid($uid)
	{
		global $DB, $L, $G;
		$sql = 'SELECT count(*) as cnt FROM bag WHERE uid='.$uid.' and status=1';
		$usedGrids = $DB->getValue($sql);
		if (false === $usedGrids) {
			return 0;
		}
		$sql = 'SELECT bag_max_grid FROM user WHERE uid='.$uid;
		$bagMaxGrid = $DB->getValue($sql);
		if ($usedGrids >= $bagMaxGrid) {
			return 0;
		} else {
			return ($bagMaxGrid - $usedGrids);
		}
	}

	/**
	 * 获取指定仓库物品的信息
	 * @param int $uid
	 * @param int $bag_id
	 */
	public static function getInfo($uid, $bag_id)
	{
		global $DB, $L, $G;
		
		$bag_id = (int)$bag_id;
		
		$sql = 'SELECT * FROM bag WHERE bag_id='.$bag_id;
		$info = $DB->getValue($sql);
		if (!$info) {
			return array(
				'val' => false,
				'msg' => $L['Unknow_item']
			);
		}
		
		// 不是自己的物品
		if ($uid != $info['uid']) {
			return array(
				'val' => false,
				'msg' => $L['Invalid_uid']
			);
		}

		return $info;
	}
	
	/**
	 * 废弃背包 物品
	 * @param int $uid
	 * @param int $bag_id
	 */
	public static function trash($uid, $bag_id)
	{
		global $DB, $L, $G;
		
		$sql = 'SELECT bag_id,uid FROM bag WHERE bag_id='.$bag_id;
		$info = $DB->getValue($sql);
		if (!$info || $info['uid']!=$uid) {
			return array(
				'val' => false,
				'msg' => $L['Invalid_uid']
			);
		}
		
		$DB->beginTransaction();
		
		$where = 'bag_id='.$bag_id;
		$ret = $DB->delete('bag', $where);
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			$DB->commit();
		} else {
			$DB->rollBack();
			$result['msg'] = $L['Failure_Update'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('bag_trash', $str);
		}

		return $result;
	}
}










